package com.hxd.mall.module.product.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.hxd.mall.R;
import com.hxd.mall.module.product.home.bean.ListBean;
import com.hxd.mall.utils.open.StringUtil;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @author zhaoyong
 * @description:
 * @date :2019-10-14 18:54
 */
class ProductSecoundListAdapter extends RecyclerView.Adapter<ProductSecoundListAdapter.MyViewHolder> {
    private static final String TAG = "MyRecyclerAdapter";

    private List<ListBean> mData;
    private Context mContext;
    private LayoutInflater inflater;

    public ProductSecoundListAdapter(Context context, List<ListBean> data) {
        this.mContext = context;
        this.mData = data;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmData(List<ListBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public void onViewRecycled(MyViewHolder holder) {
        super.onViewRecycled(holder);
        Log.d(TAG, "onViewRecycled: " + holder.title.getText().toString() + ", position: " + holder.getAdapterPosition());

//        AsyncTask asyncTask = (AsyncTask) holder.productPic.getTag(holder.productPic.getId());
//        asyncTask.cancel(true);

        RequestManager requestManager = (RequestManager) holder.productPic.getTag(holder.productPic.getId());
        requestManager.clear(holder.productPic);
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: 验证是否重用了");
        Log.d(TAG, "onBindViewHolder: 重用了" + holder.title.getTag());
        Log.d(TAG, "onBindViewHolder: 放到了" + mData.get(position));
        holder.title.setText(mData.get(position).getTitle());

        StringUtil.INSTANCE.setIconText(holder.title, holder.iv_logo_icon, mData.get(position).getSubChannel(), mData.get(position).getTitle());

        holder.title.setTag(mData.get(position));


        //先设置图片占位符
        holder.productPic.setImageDrawable(mContext.getDrawable(R.drawable.banner_default_icon));
//        @SuppressLint("StaticFieldLeak")
//        AsyncTask asyncTask = new AsyncTask<Void, Void, Bitmap>() {
//            @Override
//            protected Bitmap doInBackground(Void... params) {
//                try {
//                    URL url = new URL(mData.get(position).getPictUrl());
//                    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
//                    return bitmap;
//                } catch (MalformedURLException e) {
//                    e.printStackTrace();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return null;
//            }
//
//            @Override
//            protected void onPostExecute(Bitmap bitmap) {
//                super.onPostExecute(bitmap);
//                holder.productPic.setImageBitmap(bitmap);
//            }
//        };
        RequestManager requestManager = Glide.with(mContext);

        holder.productPic.setTag(holder.productPic.getId(), requestManager);

        requestManager.load(mData.get(position).getPictUrl()).into(holder.productPic);

//        asyncTask.execute();
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder");
        View view = inflater.inflate(R.layout.adapter_product_item_layout, parent, false);
        return new MyViewHolder(view);
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView productPic, iv_logo_icon;

        public MyViewHolder(View view) {
            super(view);
            productPic = view.findViewById(R.id.productPic);
            iv_logo_icon = view.findViewById(R.id.iv_logo_icon);
            title = view.findViewById(R.id.title);
        }
    }
}